package edu.core.testapp.server;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import edu.core.testapp.client.StoredFileService;

public class StoredFilesImpl extends RemoteServiceServlet implements StoredFileService {

	@Override
	public List<String> getStoredFiles() {
		ArrayList<String> als = new ArrayList<String>();
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		try {
			Query q = pm.newQuery(StoredFile.class);
			
			List<StoredFile> result = (List<StoredFile>) q.execute();
			
			for (StoredFile f : result)
				als.add(f.getName());
			
			Calendar gr = Calendar.getInstance();
			
			StoredFile newFile = new StoredFile(gr.getTime().toString());
			
			newFile.setData(DateFormat.getInstance().format(gr.getTime()).getBytes());
			
			pm.makePersistent(newFile);
		} finally {
			pm.close();
		}
		
		return als;
	}

}
